package com.ruijie.acl.web.validator;

import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.apache.commons.lang.StringUtils;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.faces.Validator;

import com.ruijie.acl.utils.AclUtil;
import com.ruijie.foundation.international.MessageSupport;

@Name("wildcardValidator")
@Validator
public class WildcardValidator  extends MessageSupport implements javax.faces.validator.Validator {

	@In private Map<String, String> messages;
	@Override
	public void validate(FacesContext arg0, UIComponent arg1, Object value) throws ValidatorException {
		String text = value.toString().trim();

		if(arg1.getAttributes().get("required").toString().equals("true")){
			if(!StringUtils.isNotEmpty(text.replaceAll("　",   " ").trim())){
				throw new ValidatorException(new FacesMessage(messages.get("javax.faces.component.UIInput.REQUIRED")));
			}
		}
		
		Boolean valid =AclUtil.wildcardValidator(text);
		if (!valid) {
			throw new ValidatorException(new FacesMessage(getI18NMessage("acl_ace_validation_wildcardValidateFailed")));
		}
	}
}
